package alogrithm;


/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/16 10:24
 */
class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
}

public class LowestCommonAncestor {

    private Integer ret;

    public int lowestCommonAncestor(TreeNode root, int o1, int o2) {
        // write code here
        dfs(root, o1, o2);
        return ret;
    }

    public boolean dfs(TreeNode root, int o1, int o2) {
        if (root == null) {
            return false;
        }
        boolean lson = dfs(root.left, o1, o2);
        boolean rson = dfs(root.right, o1, o2);
        if ((lson && rson) || ((root.val == o1 || root.val == o2) && (lson || rson))) {
            ret = root.val;
        }
        return lson || rson || (root.val == o1) || (root.val == o2);
    }
}
